//
//  YWStaticLoader.m
//  YWStaticLoader
//
//  Created by slz on 15/11/24.
//  Copyright © 2015年 youyuwei. All rights reserved.
//
#define RGBACOLOR(r, g, b, a)   [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)]
#import "YWStaticLoader.h"
#import "TGMActivityIndicatorView.h"

@implementation YWStaticLoader
{
    UILabel *msgLabel;
    UIView *whiteView;
    TGMActivityIndicatorView * _indicatorView;
}
-(id)initWithLoadingMsg:(NSString *)loadingMsg{
    self = [super init];
    if (self){
        self.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
        self.backgroundColor = RGBACOLOR(0, 0, 0, .4);
        _indicatorView = [TGMActivityIndicatorView defaultActivityIndicatorView];
        [self setupLoadingLabelWithMsg:loadingMsg];
    }
    return self;
}

-(void)setLoadingMsg:(NSString *)loadingMsg{
    msgLabel.text = loadingMsg;
}

- (void)setupLoadingLabelWithMsg:(NSString *)msg{
    whiteView = [[UIView alloc]initWithFrame:CGRectMake(15, kScreenHeight / 2 - 30, kScreenWidth - 30, 100)];
    whiteView.backgroundColor = [UIColor whiteColor];
    whiteView.layer.cornerRadius = 15;
    whiteView.layer.masksToBounds = YES;
    [self addSubview:whiteView];
    
    _indicatorView.center = CGPointMake(whiteView.width/2 - 10, 40);
    [_indicatorView startAnimating];
    [whiteView addSubview:_indicatorView];
    
    msgLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, whiteView.width, 20)];
    msgLabel.textAlignment = NSTextAlignmentCenter;
    msgLabel.textColor = [UIColor blackColor];
    msgLabel.font = [UIFont boldSystemFontOfSize:15];
    msgLabel.text = msg;
    msgLabel.center = CGPointMake(whiteView.width/2, 70);
    [whiteView addSubview:msgLabel];
}

- (void)showInView:(UIView *)view isRoot:(BOOL)isRoot{
    [view endEditing:YES];
    if(view!=nil && isRoot){
        [[UIApplication sharedApplication].delegate.window.rootViewController.view addSubview:self];
    }else{
        [view addSubview:self];
    }
}

- (void)hidden{
    [_indicatorView stopLoading];
    [self removeFromSuperview];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
